﻿using IXION.BLOG.DOMAIN.TAG;
using IXION.BLOG.SERVICE.ARTICLE;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IXION.BLOG.WEB.UI
{
    public partial class Search : PageBase
    {
        /// <summary>
        /// 文章列表
        /// </summary>
        public IList<IXION.BLOG.DOMAIN.BLOG.Article> articles;
        /// <summary>
        /// 页码
        /// </summary>
        public int page = 1;
        /// <summary>
        /// 分页路径
        /// </summary>
        public string pageUrl = "Default.aspx?page=";
        /// <summary>
        /// 分页条页数
        /// </summary>
        public int pagerLength = 7;
        /// <summary>
        /// 文章总数
        /// </summary>
        public int articleCount;
        /// <summary>
        /// 每页文章数
        /// </summary>
        public int pageSize;
        /// <summary>
        /// 搜索字段
        /// </summary>
        public string key;
        /// <summary>
        /// 搜索字段值
        /// </summary>
        public string value;
        public bool searchShow = false;
        /// <summary>
        /// 业务处理对象
        /// </summary>
        protected ArticleService service = new ArticleService();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Header.Title = "搜索结果——" + blog.BlogName;
            pagerLength = blog.CommonSetting.PagerBarLength;
            pageSize = blog.CommonSetting.ArticleCountPerPage;

            //搜索方式 日期 范围搜索 文章名称/内容 模糊搜索
            key = Request["key"];
            value = Request["value"];
            string p = Request["page"];
            if (key == null)
            {
                searchShow = true;
                Pager.pageCount = 0;
                return;
            }
            if (p == null || p.Trim() == "" || !Regex.IsMatch(p, REG_PAGE))
            {
                p = "1";
            }
            page = int.Parse(p);
            int start = (page - 1) * pageSize;
            //需要处理各种搜索条件下的结果数
            switch (key)
            {
                case "day":
                    articleCount = service.CountArticlesByDay(value);
                    articles = service.SearchArticlesByPostDay(value, start, pageSize);
                    break;
                case "search":
                    articleCount = service.CountArticlesByKey(value);
                    articles = service.SearchArticlesByKey(value, start, pageSize);
                    break;
                default:
                    articleCount = service.CountArticlesByDay(value);
                    articles = service.SearchArticlesByPostDay(value, start, pageSize);
                    break;
            }
            pageUrl = "Search.aspx?key=" + key + "&value=" + value + "&page=";
            ArticleRepeater.DataSource = articles;
            ArticleRepeater.DataBind();
            FillPager();
        }
        private void FillPager()
        {
            Pager.curPage = page;
            Pager.pageUrl = pageUrl;
            Pager.pageCount = articleCount % pageSize == 0 ? articleCount / pageSize : articleCount / pageSize + 1;
            Pager.length = pagerLength;
            //throw new NotImplementedException();
        }
    }
}